﻿@model Catalog
@Html.Extra()
@{
    Layout = "~/Views/Shared/OA/_Form.cshtml";

    var isAdmin = ManagerAuthorization.Current.User.IsAdmin;

    if (Model.Type == 0)
    {
        Model.Type = (int)CatalogType.Article;
    }
}
@section Toolbar
{
    @Html.MyNet().Button(new ButtonOptions { Text = "保存", Click = "save" })
    @Html.MyNet().Button(new ButtonOptions { Text = "返回", Click = "back" })
}
@section Main
{
    @Html.HiddenFor(model => model.Id)
    @Html.HiddenFor(model => model.Path)
    @Html.HiddenFor(model => model.SortIndex)
    @Html.HiddenFor(model => model.Config)
    @Html.HiddenFor(model => model.Sitemap)
    <fieldset>
        <legend>基本设置</legend>
        <table>
            @if (isAdmin)
            {
                <tr>
                    <th>
                        @Html.LabelFor(model => model.ParentId, "父级")
                    </th>
                    <td>
                        @Html.MyNet().DropDownTreeFor(model => model.ParentId, Url.Action("Read"))
                    </td>
                </tr>
            }
            else
            {
                @Html.HiddenFor(model => model.ParentId)
            }

            <tr>
                <th>
                    @Html.LabelFor(model => model.Name, "名称")
                </th>
                <td>
                    @Html.MyNet().TextBoxFor(model => model.Name, new ValidateOptions
                    {
                        RequiredError = "请输入名称",
                        LengthMax = 64
                    })
                    @Html.MyNet().ValidationMessageFor(model => model.Name)
                </td>
            </tr>
            <tr>
                <th>
                    @Html.LabelFor(model => model.Key, "代码")
                </th>
                <td>
                    @Html.MyNet().TextBoxFor(model => model.Key, new ValidateOptions
                    {
                        RequiredError = "请输入代码",
                        LengthMax = 64
                    })
                    @Html.MyNet().ValidationMessageFor(model => model.Key)
                </td>
            </tr>
            @if (isAdmin)
            {

                <tr>
                    <th>
                        @Html.LabelFor(model => model.Type, "类型")
                    </th>
                    <td>
                        @Html.MyNet().DropDownListFor(model => model.Type, new[]
                                                                                            {
                    new { Value = (int)CatalogType.Article, Name = CatalogType.Article.ToText() },
                    new { Value = (int)CatalogType.ArticleDetail, Name = CatalogType.ArticleDetail.ToText() },
                    new { Value = (int)CatalogType.Image, Name = CatalogType.Image.ToText() },
                    new { Value = (int)CatalogType.Advertisement, Name = CatalogType.Advertisement.ToText() },
                    new { Value = (int)CatalogType.FriendshipLink, Name = CatalogType.FriendshipLink.ToText() },
                    //new { Value = (int)CatalogType.Vote, Name = CatalogType.Vote.ToText() },
                    //new { Value = (int)CatalogType.Other, Name = CatalogType.Other.ToText()}
                    }, "Value", "Name", "请选择类型", new ValidateOptions
                    {
                        RequiredError = "请选择类型"
                    })
                        @Html.MyNet().ValidationMessageFor(model => model.Type)
                    </td>
                </tr>
            }
            else
            {
                @Html.HiddenFor(model => model.Type)
            }
            <tr>
                <th>
                    @Html.LabelFor(model => model.ImageUrl, "图片")
                </th>
                <td>
                    @Html.MyNet().UploadFor(model => model.ImageUrl, new UploadOptions
                    {
                        AttachmentKey = Model.Id,
                        AttachmentTypeKey = "CMS_CatalogImage",
                        UploadMode = UploadMode.ImageSingle,
                        PreviewImageWidth = 300
                    })
                </td>
            </tr>
            <tr>
                <th>
                    @Html.LabelFor(model => model.BannerUrl, "导航图片")
                </th>
                <td>
                    @Html.MyNet().UploadFor(model => model.BannerUrl, new UploadOptions
                    {
                        AttachmentKey = Model.Id,
                        AttachmentTypeKey = "CMS_CatalogBannerImage",
                        UploadMode = UploadMode.ImageSingle,
                        PreviewImageWidth = 600
                    })
                </td>
            </tr>

            <tr>
                <th>
                    @Html.LabelFor(model => model.Keyword, "关键字")
                </th>
                <td>
                    @Html.MyNet().TextAreaFor(model => model.Keyword, new ValidateOptions
                    {
                        LengthError = "不能超过256字符",
                        LengthMax = 256
                    })
                    @Html.MyNet().ValidationMessageFor(model => model.Description)
                </td>
            </tr>
            <tr>
                <th>
                    @Html.LabelFor(model => model.Description, "描述")
                </th>
                <td>
                    @Html.MyNet().TextAreaFor(model => model.Description, new ValidateOptions
                    {
                        LengthError = "不能超过256字符",
                        LengthMax = 256
                    })
                    @Html.MyNet().ValidationMessageFor(model => model.Description)
                </td>
            </tr>
            <tr>
                <th></th>
                <td>
                    @Html.CheckBoxFor(model => model.IsEnable)
                    @Html.LabelFor(model => model.IsEnable, "是否启用")
                </td>
            </tr>
        </table>
    </fieldset>

    <fieldset>
        <legend>导航设置</legend>
        <table>
            <tr>
                <th>
                    @Html.LabelFor(model => model.LinkUrl, "导航地址")
                </th>
                <td>
                    @Html.MyNet().TextBoxFor(model => model.LinkUrl)
                </td>
            </tr>
            <tr>
                <th></th>
                <td>
                    @Html.MyNet().CheckBoxFor(model => model.IsNav)
                    @Html.LabelFor(model => model.IsNav, "是否导航")
                </td>
            </tr>
        </table>
    </fieldset>

}
<script>

    function save() {
        var form = $("form");
        if (!form.valid()) return;

        $.ajax
            ({
                url: form.attr("action"),
                data: form.serialize(),
                type: "post",
                error: disposeError,
                success: function (result) {
                    if (disposeException(result)) return;
                    var data = { id: result.Id, text: result.Name, parentId: result.ParentId };
                    back(true, data);
                }
            });
    }

</script>
